//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.yaukie.base.core.service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.yaukie.base.util.SpringContextUtil;

@Transactional(
        propagation = Propagation.REQUIRED,
        rollbackFor = {RuntimeException.class}
)
public abstract class BaseService<M, T, E> implements Service<T, E> {
    private static final Logger log = LoggerFactory.getLogger(BaseService.class);
    public M m;

    public BaseService() {
    }

    public long countByExample(E example) {
        try {
            Method countByExample = this.m.getClass().getDeclaredMethod("countByExample", example.getClass());
            T obj = (T) countByExample.invoke(this.m, example);
            return Long.parseLong(String.valueOf(obj));
        } catch (IllegalAccessException var4) {
            log.error("{}countByExample.IllegalAccessException", var4);
            throw new RuntimeException("SQL执行出错");
        } catch (InvocationTargetException var5) {
            log.error("{}countByExample.InvocationTargetException", var5);
            throw new RuntimeException("SQL执行出错");
        } catch (NoSuchMethodException var6) {
            log.error("{}countByExample.NoSuchMethodException", var6);
            throw new RuntimeException("SQL执行出错");
        }
    }

    public int deleteByExample(E example) {
        try {
            Method deleteByExample = this.m.getClass().getDeclaredMethod("deleteByExample", example.getClass());
            T obj = null;
            obj = (T) deleteByExample.invoke(this.m, example);
            return Integer.parseInt(String.valueOf(obj));
        } catch (IllegalAccessException var4) {
            log.error("{}deleteByExample.IllegalAccessException", var4);
            throw new RuntimeException("SQL执行出错" + var4.getMessage());
        } catch (InvocationTargetException var5) {
            log.error("{}deleteByExample.InvocationTargetException", var5);
            throw new RuntimeException("SQL执行出错" + var5.getMessage());
        } catch (NoSuchMethodException var6) {
            log.error("{}deleteByExample.NoSuchMethodException", var6);
            throw new RuntimeException("SQL执行出错" + var6.getMessage());
        }
    }

    public int deleteByPrimaryKey(Integer id) {
        try {
            Method deleteByPrimaryKey = this.m.getClass().getDeclaredMethod("deleteByPrimaryKey", id.getClass());
            T obj = (T) deleteByPrimaryKey.invoke(this.m, id);
            return Integer.parseInt(String.valueOf(obj));
        } catch (IllegalAccessException var4) {
            log.error("{}deleteByPrimaryKey.IllegalAccessException", var4);
            throw new RuntimeException("SQL执行出错");
        } catch (InvocationTargetException var5) {
            log.error("{}deleteByPrimaryKey.InvocationTargetException", var5);
            throw new RuntimeException("SQL执行出错");
        } catch (NoSuchMethodException var6) {
            log.error("{}deleteByPrimaryKey.NoSuchMethodException", var6);
            throw new RuntimeException("SQL执行出错");
        }
    }

    public int insert(T record) {
        try {
            Method insert = this.m.getClass().getDeclaredMethod("insert", record.getClass());
            T obj  = (T) insert.invoke(this.m, record);
            return Integer.parseInt(String.valueOf(obj));
        } catch (IllegalAccessException var4) {
            log.error("{}insert.IllegalAccessException", var4);
            throw new RuntimeException("SQL执行出错");
        } catch (InvocationTargetException var5) {
            log.error("{}insert.InvocationTargetException", var5);
            throw new RuntimeException("SQL执行出错");
        } catch (NoSuchMethodException var6) {
            log.error("{}insert.NoSuchMethodException", var6);
            throw new RuntimeException("SQL执行出错");
        }
    }

    public int insertSelective(T record) {
        try {
            Method insertSelective = this.m.getClass().getDeclaredMethod("insertSelective", record.getClass());
            T obj  = (T) insertSelective.invoke(this.m, record);
            return Integer.parseInt(String.valueOf(obj));
        } catch (IllegalAccessException var4) {
            log.error("{}insertSelective.IllegalAccessException", var4);
            throw new RuntimeException("SQL执行出错" + var4.getMessage());
        } catch (InvocationTargetException var5) {
            log.error("{}insertSelective.InvocationTargetException", var5);
            throw new RuntimeException("SQL执行出错" + var5.getMessage());
        } catch (NoSuchMethodException var6) {
            log.error("{}insertSelective.NoSuchMethodException", var6);
            throw new RuntimeException("SQL执行出错" + var6.getMessage());
        }
    }

    public List<T> selectByExample(E example) {
        try {
            Method selectByExample = this.m.getClass().getDeclaredMethod("selectByExample", example.getClass());
            List<T> obj = null;
            obj = (List) selectByExample.invoke(this.m, example);
            return obj;
        } catch (IllegalAccessException var4) {
            log.error("{}selectByExample.IllegalAccessException", var4);
            throw new RuntimeException("SQL执行出错");
        } catch (InvocationTargetException var5) {
            log.error("{}selectByExample.InvocationTargetException", var5);
            throw new RuntimeException("SQL执行出错");
        } catch (NoSuchMethodException var6) {
            log.error("{}selectByExample.NoSuchMethodException", var6);
            throw new RuntimeException("SQL执行出错");
        }
    }

    public T selectByPrimaryKey(Integer id) {
        try {
            Method selectByPrimaryKey = this.m.getClass().getDeclaredMethod("selectByPrimaryKey", id.getClass());
            T obj  = (T) selectByPrimaryKey.invoke(this.m, id);
            return obj;
        } catch (IllegalAccessException var4) {
            log.error("{}selectByPrimaryKey.IllegalAccessException", var4);
            throw new RuntimeException("SQL执行出错");
        } catch (InvocationTargetException var5) {
            log.error("{}selectByPrimaryKey.InvocationTargetException", var5);
            throw new RuntimeException("SQL执行出错");
        } catch (NoSuchMethodException var6) {
            log.error("{}selectByPrimaryKey.NoSuchMethodException", var6);
            throw new RuntimeException("SQL执行出错");
        }
    }

    public T selectFirstExample(E example) {
        try {
            Method selectByExample = this.m.getClass().getDeclaredMethod("selectByExample", example.getClass());
            List<T> result = (List) selectByExample.invoke(this.m, example);
            return result != null && result.size() > 0 ? result.get(0) : null;
        } catch (IllegalAccessException var4) {
            log.error("{}selectFirstExample.IllegalAccessException", var4);
            throw new RuntimeException("SQL执行出错");
        } catch (InvocationTargetException var5) {
            log.error("{}selectFirstExample.InvocationTargetException", var5);
            throw new RuntimeException("SQL执行出错");
        } catch (NoSuchMethodException var6) {
            log.error("{}selectFirstExample.NoSuchMethodException", var6);
            throw new RuntimeException("SQL执行出错");
        }
    }

    public int updateByExampleSelective(@Param("record") T record, @Param("example") E example) {
        try {
            Method updateByExampleSelective = this.m.getClass().getDeclaredMethod("updateByExampleSelective", record.getClass(), example.getClass());
            T obj = (T) updateByExampleSelective.invoke(this.m, record, example);
            return Integer.parseInt(String.valueOf(obj));
        } catch (IllegalAccessException var5) {
            log.error("{}updateByExampleSelective.IllegalAccessException", var5);
            throw new RuntimeException("SQL执行出错");
        } catch (InvocationTargetException var6) {
            log.error("{}updateByExampleSelective.InvocationTargetException", var6);
            throw new RuntimeException("SQL执行出错");
        } catch (NoSuchMethodException var7) {
            log.error("{}updateByExampleSelective.NoSuchMethodException", var7);
            throw new RuntimeException("SQL执行出错");
        }
    }

    public int updateByExample(@Param("record") T record, @Param("example") E example) {
        try {
            Method updateByExample = this.m.getClass().getDeclaredMethod("updateByExample", record.getClass(), example.getClass());
            T obj = (T) updateByExample.invoke(this.m, record, example);
            return Integer.parseInt(String.valueOf(obj));
        } catch (IllegalAccessException var5) {
            log.error("{}updateByExample.IllegalAccessException", var5);
            throw new RuntimeException("SQL执行出错");
        } catch (InvocationTargetException var6) {
            log.error("{}updateByExample.InvocationTargetException", var6);
            throw new RuntimeException("SQL执行出错");
        } catch (NoSuchMethodException var7) {
            log.error("{}updateByExample.NoSuchMethodException", var7);
            throw new RuntimeException("SQL执行出错");
        }
    }

    public int updateByPrimaryKeySelective(T record) {
        try {
            Method updateByPrimaryKeySelective = this.m.getClass().getDeclaredMethod("updateByPrimaryKeySelective", record.getClass());
            T obj = (T) updateByPrimaryKeySelective.invoke(this.m, record);
            return Integer.parseInt(String.valueOf(obj));
        } catch (IllegalAccessException var4) {
            log.error("{}updateByPrimaryKeySelective.IllegalAccessException", var4);
            throw new RuntimeException("SQL执行出错");
        } catch (InvocationTargetException var5) {
            log.error("{}updateByPrimaryKeySelective.InvocationTargetException", var5);
            throw new RuntimeException("SQL执行出错");
        } catch (NoSuchMethodException var6) {
            log.error("{}updateByPrimaryKeySelective.NoSuchMethodException", var6);
            throw new RuntimeException("SQL执行出错");
        }
    }

    public int updateByPrimaryKey(T record) {
        try {
            Method updateByPrimaryKey = this.m.getClass().getDeclaredMethod("updateByPrimaryKey", record.getClass());
            T obj = (T) updateByPrimaryKey.invoke(this.m, record);
            return Integer.parseInt(String.valueOf(obj));
        } catch (IllegalAccessException var4) {
            log.error("{}updateByPrimaryKey.IllegalAccessException", var4);
            throw new RuntimeException("SQL执行出错");
        } catch (InvocationTargetException var5) {
            log.error("{}updateByPrimaryKey.InvocationTargetException", var5);
            throw new RuntimeException("SQL执行出错");
        } catch (NoSuchMethodException var6) {
            log.error("{}updateByPrimaryKey.NoSuchMethodException", var6);
            throw new RuntimeException("SQL执行出错");
        }
    }

    public void initMapper() {
        this.m = this.getMapperClass() == null ? null : SpringContextUtil.getBean(this.getMapperClass());
    }

    public Class<M> getMapperClass() {
        Type type = this.getClass().getGenericSuperclass();
        log.info("当前父类的参数化类型，{}", type);
        ParameterizedType parameterizedType = null;
        if (null != type) {
            parameterizedType = (ParameterizedType) type;
            Class cls = (Class) parameterizedType.getActualTypeArguments()[0];
            return cls;
        } else {
            return null;
        }
    }
}
